﻿using Tfs.OnTheRoad.Commands;
using Tfs.OnTheRoad.Model;
namespace Tfs.OnTheRoad.ViewModel
{
    /// <summary>
    /// Project Overview ViewModel
    /// </summary>
    public class ProjectOverviewViewModel : BaseViewModel
    {

        public override void Refresh()
        {
            //We just refresh what was already loaded
            summaryLoaded = false;//Summary forced on refresh
            LoadSummary();
            if (_buildListViewModel.Loaded)
                _buildListViewModel.LoadData();
            if (_changesetListViewModel.Loaded)
            _changesetListViewModel.LoadData();
            if (_branchListViewModel.Loaded)
            _branchListViewModel.LoadData();
            if(_queryListViewModel.Loaded)
            _queryListViewModel.LoadData();
        }

        public NavigateToWorkItemsOverviewCommand NavigateToWorkItemsOverview
        {
            get;
            private set;
        }
        

        private Project _currentProject;

        public Project CurrentProject
        {
            get { return _currentProject; }
            set { _currentProject = value; }
        }

        private bool _processing;

        public bool Processing
        {
            get { return _processing; }
            set
            {
                _processing = value;
                this.RaisePropertyChanged("Processing");
            }
        }

        private BuildListViewModel _buildListViewModel;

        public BuildListViewModel BuildListViewModel
        {
            get { return _buildListViewModel; }
            set { _buildListViewModel = value; }
        }

        private ChangesetListViewModel _changesetListViewModel;

        public ChangesetListViewModel ChangesetListViewModel
        {
            get { return _changesetListViewModel; }
            set { _changesetListViewModel = value; }
        }


        private BranchListViewModel _branchListViewModel;

        public BranchListViewModel BranchListViewModel
        {
            get { return _branchListViewModel; }
            set { _branchListViewModel = value; }
        }

        private QueriesListViewModel _queryListViewModel;

        public QueriesListViewModel QueryListViewModel
        {
            get { return _queryListViewModel; }
            set { _queryListViewModel = value; }
        }

        private WorkItemsChartViewModel _workItemsChartViewModel;

        public WorkItemsChartViewModel WorkItemsChartViewModel
        {
            get { return _workItemsChartViewModel; }
            set { _workItemsChartViewModel = value; }
        }

        public ProjectOverviewViewModel(Model.Project project)
            : base()
        {
            NavigateToWorkItemsOverview = new NavigateToWorkItemsOverviewCommand();
            PageTitle = project.Name;
            _currentProject = project;
            _workItemsChartViewModel = new ViewModel.WorkItemsChartViewModel("Summary");
            _buildListViewModel = new ViewModel.BuildListViewModel(_currentProject);
            _changesetListViewModel = new ViewModel.ChangesetListViewModel(_currentProject);
            _queryListViewModel = new ViewModel.QueriesListViewModel(_currentProject);
            _branchListViewModel = new ViewModel.BranchListViewModel(_currentProject);
            BuildListViewModel.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(ChildViewMovel_PropertyChanged);
            ChangesetListViewModel.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(ChildViewMovel_PropertyChanged);
            QueryListViewModel.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(ChildViewMovel_PropertyChanged);
            BranchListViewModel.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(ChildViewMovel_PropertyChanged);
        }

        bool summaryLoaded = false;
        internal void LoadSummary()
        {
            if (!summaryLoaded)
            {
                _workItemsChartViewModel.CalculateSummary();
                summaryLoaded = true;
            }
        }

        void ChildViewMovel_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            IBaseListViewModel baseList = sender as IBaseListViewModel;
            if (string.Compare(e.PropertyName, "IsSearching") == 0 && baseList!=null)
            {
                Processing = baseList.IsSearching;
            }
        }

        private int _workItemsCount;

        public int WorkItemsCount
        {
            get { return _workItemsCount; }
            set
            {
                _workItemsCount = value;
                this.RaisePropertyChanged("WorkItemsCount");
            }
        }
     
        bool buildsLoaded = false;
        internal void LoadBuilds()
        {
            if (!buildsLoaded)
            {
                BuildListViewModel.LoadData();
                buildsLoaded = true;
            }
        }

        bool changesetsLoaded = false;
        internal void LoadChangesets()
        {
            if (!changesetsLoaded)
            {
                ChangesetListViewModel.LoadData();
                changesetsLoaded = true;
            }
        }

        bool queriesLoaded = false;
        internal void LoadQueries()
        {
            if (!queriesLoaded)
            {
                QueryListViewModel.LoadData();
                queriesLoaded = true;
            }
        }


        bool branchesLoaded = false;
        internal void LoadBranches()
        {
            if (!branchesLoaded)
            {
                BranchListViewModel.LoadData();
                branchesLoaded = true;
            }
        }
    }
}
